    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include "../sgl.h"
    #include "../simple.h"
    /* The screen surface, joystick device */
    surface *screen = NULL;


    int main ( int argc, char *argv[])
    {
    	int done;
        int x;
	int width;
	int height;
	    
	    
	 width=640;
	height=480;

        screen=sgl_start(width,height);
        setfont( "media/DejaVuSans.ttf");
        surface *txt_sgl=text( "welcome in SGL demo", 60, rgb( 255, 0, 0));
        surface *txt_press_start=text( "press enter", 20, rgb( 0, 0, 255));

        surface *vroby=surface_load("media/vroby.png",SGL_ALPHA);
        sound *wav=sound_load("media/wav.wav");
 
	 music_load( "media/sgl.mod");
        music_play( 127,-1);

        for(x=-10;x<120;x++){
            cls(screen,rgb(255,255,255));
            paste(screen,10,x,txt_sgl);
            screenswap();
        }

        paste(screen,10,220,txt_press_start);

        while(!keystate[13])screenswap();
        while(keystate[13])screenswap();
        sound_play(wav,1,64,0);
        alpha( screen,0);
        cls(screen,rgb(255,255,255));

        for(x=0;x<64;x++){
            alpha( screen,x);
            screenswap();
        }

        cls(screen,0);
        alpha( screen,0);
        for(x=0;x<64;x++){
            alpha( screen,x);
            screenswap();
        }
        alpha( screen,255);

    	done = 0;
 
        cls(screen,rgb(255,255,255));
        paste(screen,10,220,txt_press_start);
        while(keystate[13]) screenswap();
        done=0;
        while(done!=2){
                line( screen, rand()%width, rand()%height, rand()%width, rand()%height, rgb(rand()%255,rand()%255,rand()%255));
                screenswap();
                if (keystate[13])done=2;
        }
        sound_play(wav,1,64,0);

        cls(screen,rgb(255,255,255));
        paste(screen,10,220,txt_press_start);
        while(keystate[13]) screenswap();
        done=0;
        while(done!=2){
                box( screen, rand()%width, rand()%height, rand()%width, rand()%height, rgb(rand()%255,rand()%255,rand()%255));
                screenswap();
                if (keystate[13])done=2;
                if (keystate[27])exit(2);

        }
        sound_play(wav,1,64,0);

        cls(screen,rgb(255,255,255));
        paste(screen,10,220,txt_press_start);
        while(keystate[13]) screenswap();
        done=0;
        while(done!=2){
                bar( screen, rand()%width, rand()%height, rand()%width, rand()%height, rgb(rand()%255,rand()%255,rand()%255));
                screenswap();
                if (keystate[13])done=2;
                if (keystate[27])exit(2);

        }
        sound_play(wav,1,64,0);

        cls(screen,rgb(255,255,255));
        paste(screen,10,220,txt_press_start);
        while(keystate[13]) screenswap();
        done=0;
        while(done!=2){
                circle( screen, rand()%width, rand()%height, rand()%100, rgb(rand()%255,rand()%255,rand()%255));
                screenswap();
                if (keystate[13])done=2;
		if (keystate[27])exit(2);
        }
        sound_play(wav,1,64,0);

        cls(screen,rgb(255,255,255));
        paste(screen,10,220,txt_press_start);
        while(keystate[13]) screenswap();
        done=0;
        while(done!=2){
                fillcircle( screen, rand()%width, rand()%height, rand()%100, rgb(rand()%255,rand()%255,rand()%255));
                screenswap();
                if (keystate[13])done=2;
		if (keystate[27])exit(2);

        }
        sound_play(wav,1,64,0);

        cls(screen,rgb(255,255,255));
        paste(screen,10,220,txt_press_start);
        while(keystate[13]) screenswap();
        done=0;
        while(done!=2){
                ellipse( screen, rand()%width, rand()%height, rand()%width, rand()%height, rgb(rand()%255,rand()%255,rand()%255));
                screenswap();
                if (keystate[13])done=2;
		if (keystate[27])exit(2);
        }
        sound_play(wav,1,64,0);

        cls(screen,rgb(255,255,255));
        paste(screen,10,220,txt_press_start);
        while(keystate[13]) screenswap();
        done=0;
        while(done!=2){
                fillellipse( screen, rand()%width, rand()%height, rand()%width, rand()%height, rgb(rand()%255,rand()%255,rand()%255));
                screenswap();
                if (keystate[13])done=2;
		if (keystate[27])exit(2);
        }
        sound_play(wav,1,64,0);

        cls(screen,rgb(0,0,255));
        while(keystate[13]) screenswap();
        done=0;
        while(done!=2){
            for(x=-64;x<width;x+=1){
                cls(screen,rgb(0,0,255));
                paste(screen,x,100,vroby);
                paste(screen,10,220,txt_press_start);
                if (keystate[13])done=1;
                if (!keystate[13]&& done==1)done=2;
		 if (keystate[27])exit(2);   
                if (done==2)break;
                screenswap();
            }
        }
        sound_play(wav,1,64,0);

        cls(screen,rgb(0,0,255));
        while(keystate[13]) screenswap();
        done=0;
    #define D 1024
        int scroll_x[D];
        int scroll_y[D];
        for( x=0;x<D;x++){
             scroll_x[x]=rand()%width;
             scroll_y[x]=(rand()%height)-32;
        }

        while(done!=2){
            cls(screen,rgb(0,0,255));
            paste(screen,10,220,txt_press_start);
            for( x=0;x<D;x++){
                scroll_x[x]+=( x / (D/5))+1;
                scroll_x[x]%=(width+32);
                paste(screen,(scroll_x[x])-32,scroll_y[x],vroby);
                if (keystate[13])done=1;
                if (!keystate[13]&& done==1)done=2;
	        if (keystate[27])exit(2);
            }
            if (done==2)break;
            screenswap();
        }
        sound_play(wav,1,64,0);

        set_colorkey(0);
        surface *s_x=surface_create(64,64,1);
        line(s_x,0,64,64,0,rgb(255,255,255));
        line(s_x,0,0,64,64,rgb(255,255,255));

        surface *s_y=surface_create(64,64,1);
        line(s_y,0,32,64,32,rgb(255,255,255));
        line(s_y,32,0,32,64,rgb(255,255,255));
        surface *txt_collision=text("collisione",16,rgb(255,0,0));

        done=0;
      while(done!=2){
            for( x=-32;x<(width+32);x++){
                cls(screen,rgb(128,128,128));
                paste(screen,10,220,txt_press_start);
                paste(screen,160,120,s_x);
                paste(screen,x,120,s_y);
                if (hit(s_x,160,120,s_y,x,120)){
                   paste(screen,x,200,txt_collision);
                }
                screenswap();
                if (keystate[13])done=1;
                if (!keystate[13]&& done==1)done=2;
                if (done==2)break;
            }
        }
        sound_play(wav,1,64,0);



        cls(screen,rgb(0,0,0));
        for(x=music_volume(-1);x>0;x--){
            alpha(screen, (255-x));
            music_volume(x);
            screenswap();
        }
	sgl_terminate();
    	return 0;
    }
